package com.blb.generator.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.generator.entity.Staff;
import com.blb.generator.entity.dto.StaffDto;
import com.blb.generator.service.AdminService;
import com.blb.generator.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-02-21
 */
@Controller
@RequestMapping("/generator/staff")
public class StaffController {
    private final AdminService adminService;

    private final StaffService staffService;

    public StaffController(StaffService staffService, AdminService adminService) {
        this.staffService = staffService;
        this.adminService = adminService;
    }

    @GetMapping("/myLogin")
    public String myLogin(){
        return "myLogin";
    }

    @PostMapping("/check")
    public String check(Staff staff, Model model){
//        Wrapper<Staff> staffWrapper = new Wrapper<>();
        QueryWrapper<Staff> staffQueryWrapper = new QueryWrapper<>();
        staffQueryWrapper.eq("username",staff.getUsername());
        staffQueryWrapper.eq("password",staff.getPassword());
        Staff one = staffService.getOne(staffQueryWrapper);
        if(one==null){
            return "redirect:/generator/staff/myLogin";
        }
        model.addAttribute("staff",one);

        return "redirect:/generator/staff/list";
    }

    @GetMapping("/list")
    public String list(Model model,String  pageNum){
        model.addAttribute("msg","欢迎");
//        System.out.println("StaffController.check");
        int num=0;
        if (pageNum!=null){
            num = Integer.parseInt(pageNum);
        }
        Page<StaffDto> staffDtoPage = new Page<>(num, 3);
        model.addAttribute("staffList",staffService.getList(staffDtoPage));
        model.addAttribute("staffDtoPage",staffDtoPage);
        return "index";
    }

    @GetMapping("/toAdd")
    public String toAdd(Model model){
        model.addAttribute("adminList",adminService.list());
        return "add";
    }
    @PostMapping("/add")
    public String add(Staff staff){
        boolean save = staffService.save(staff);
        System.out.println("save = " + save);
        return "redirect:/generator/staff/list";
    }

    @GetMapping("/delete")
    public String delete(Integer id){
        boolean b = staffService.removeById(id);
        System.out.println("b = " + b);
        return "redirect:/generator/staff/list";
    }

    @GetMapping("/updatePage")
    public String updatePage(Integer id,Model model){
        model.addAttribute("adminList",adminService.list());
        model.addAttribute("id",id);
        return "update";
    }

    @PostMapping("/update")
    public String update(Staff staff){
        QueryWrapper<Staff> staffQueryWrapper = new QueryWrapper<>();
        staffQueryWrapper.eq("id",staff.getId());
        boolean update = staffService.update(staff, staffQueryWrapper);
        System.out.println("update = " + update);
        return "update";
    }
}

